home *** CD-ROM | disk | FTP | other *** search
/ Champak 45 / Vol 45.iso / games / bubblegu.swf / scripts / frame_2 / DoAction_7.as < prev    next >
Encoding:
Text File  |  2007-04-20  |  7.8 KB  |  317 lines

  1. _global.Timer = function()
  2. {
  3.    this.init();
  4. };
  5. Timer.prototype.init = function()
  6. {
  7.    var _loc1_ = this;
  8.    _loc1_.delay = 1000;
  9.    _loc1_.initialDelay = 0;
  10.    _loc1_.ticksCounter = 0;
  11.    _loc1_.hiddenTicksCounter = 0;
  12.    _loc1_.alarm = 0;
  13.    _loc1_.alarms = [];
  14.    _loc1_.sentAlarms = 0;
  15.    _loc1_._listeners = [];
  16.    _loc1_.mcs = [];
  17.    _loc1_.running = false;
  18.    _loc1_.paused = false;
  19.    _loc1_.controlling = false;
  20.    _loc1_.scheduled = false;
  21. };
  22. Timer.prototype.setDelay = function(delay)
  23. {
  24.    var _loc1_ = delay;
  25.    _loc1_ = parseInt(_loc1_);
  26.    if(isNaN(_loc1_) || _loc1_ < 1)
  27.    {
  28.       trace("Debugger ::\t(setDelay)\tThe delay value has to be a number greater than 1.");
  29.       return 0;
  30.    }
  31.    this.delay = _loc1_;
  32. };
  33. Timer.prototype.setInitialDelay = function(initialDelay)
  34. {
  35.    var _loc1_ = initialDelay;
  36.    _loc1_ = parseInt(_loc1_);
  37.    if(isNaN(_loc1_) || _loc1_ < 0)
  38.    {
  39.       trace("Debugger ::\t(setInitialDelay)\tThe initial delay value has to be a number greater than 0.");
  40.       return 0;
  41.    }
  42.    this.initialDelay = _loc1_;
  43. };
  44. Timer.prototype.getDelay = function()
  45. {
  46.    return this.delay;
  47. };
  48. Timer.prototype.getInitialDelay = function()
  49. {
  50.    return this.initialDelay;
  51. };
  52. Timer.prototype.addListener = function(reference)
  53. {
  54.    this._listeners[reference] = reference;
  55. };
  56. Timer.prototype.removeListener = function(reference)
  57. {
  58.    delete this._listeners[reference];
  59. };
  60. Timer.prototype.sendEvent = function(functionName, params)
  61. {
  62.    var _loc1_ = this;
  63.    var _loc2_ = functionName;
  64.    var _loc3_ = params;
  65.    for(var i in _loc1_._listeners)
  66.    {
  67.       _loc1_._listeners[i][_loc2_](_loc3_);
  68.    }
  69. };
  70. Timer.prototype.isRunning = function()
  71. {
  72.    return this.running;
  73. };
  74. Timer.prototype.isPaused = function()
  75. {
  76.    return this.paused;
  77. };
  78. Timer.prototype.hasSchedule = function()
  79. {
  80.    return this.scheduled;
  81. };
  82. Timer.prototype.hasControl = function()
  83. {
  84.    return this.controlling;
  85. };
  86. Timer.prototype.getControlledMovieclips = function()
  87. {
  88.    return this.mcs;
  89. };
  90. Timer.prototype.start = function()
  91. {
  92.    var _loc1_ = this;
  93.    if(_loc1_.intervalID)
  94.    {
  95.       clearInterval(_loc1_.intervalID);
  96.    }
  97.    _loc1_.intervalID = setInterval(_loc1_,"tick",_loc1_.delay);
  98.    _loc1_.running = true;
  99.    _loc1_.paused = false;
  100.    _loc1_.sendEvent("onTimerStart");
  101.    _loc1_.startTime = getTimer();
  102. };
  103. Timer.prototype.stop = function()
  104. {
  105.    var _loc1_ = this;
  106.    clearInterval(_loc1_.intervalID);
  107.    _loc1_.running = false;
  108.    _loc1_.sendEvent("onTimerStop");
  109. };
  110. Timer.prototype.pause = function()
  111. {
  112.    var _loc1_ = this;
  113.    if(_loc1_.isRunning())
  114.    {
  115.       _loc1_.paused = true;
  116.       _loc1_.sendEvent("onTimerPause");
  117.    }
  118. };
  119. Timer.prototype.sleep = function(sleepPeriod)
  120. {
  121.    var _loc1_ = this;
  122.    var _loc2_ = sleepPeriod;
  123.    _loc2_ = parseInt(_loc2_);
  124.    if(isNaN(_loc2_) || _loc2_ < 1)
  125.    {
  126.       trace("Debugger ::\t(sleep)\tThe sleepPeriod value has to be a number greater than 1.");
  127.       return 0;
  128.    }
  129.    if(_loc1_.isRunning())
  130.    {
  131.       _loc1_.paused = true;
  132.       _loc1_.sleepDuration = _loc1_.getTicksDifference() + _loc2_;
  133.       _loc1_.sendEvent("onTimerSleep");
  134.    }
  135. };
  136. Timer.prototype.resume = function()
  137. {
  138.    var _loc1_ = this;
  139.    if(_loc1_.isRunning() && _loc1_.isPaused())
  140.    {
  141.       _loc1_.paused = false;
  142.       _loc1_.sendEvent("onTimerResume");
  143.    }
  144. };
  145. Timer.prototype.reset = function()
  146. {
  147.    var _loc1_ = this;
  148.    _loc1_.ticksCounter = 0;
  149.    _loc1_.hiddenTicksCounter = 0;
  150.    _loc1_.sentAlarms = 0;
  151.    _loc1_.sendEvent("onTimerReset");
  152. };
  153. Timer.prototype.getTicksCount = function()
  154. {
  155.    return this.ticksCounter;
  156. };
  157. Timer.prototype.getHiddenTicksCount = function()
  158. {
  159.    return this.hiddenTicksCounter;
  160. };
  161. Timer.prototype.getTicksDifference = function()
  162. {
  163.    var _loc1_ = this;
  164.    _loc1_.ticksDifference = _loc1_.getHiddenTicksCount() - _loc1_.getTicksCount();
  165.    return _loc1_.ticksDifference;
  166. };
  167. Timer.prototype.setAlarm = function(alarm)
  168. {
  169.    var _loc1_ = alarm;
  170.    _loc1_ = parseInt(_loc1_);
  171.    if(isNaN(_loc1_) || _loc1_ < 1)
  172.    {
  173.       trace("Debugger ::\t(setAlarm)\tThe alarm value has to be a number greater than 1.");
  174.       return 0;
  175.    }
  176.    this.alarm = _loc1_;
  177.    this.hasAlarm = true;
  178. };
  179. Timer.prototype.getAlarm = function()
  180. {
  181.    return this.alarm + 1;
  182. };
  183. Timer.prototype.setAlarms = function()
  184. {
  185.    var _loc2_ = arguments;
  186.    var _loc3_ = this;
  187.    var _loc1_ = 0;
  188.    while(_loc1_ < _loc2_.length)
  189.    {
  190.       _loc2_[_loc1_] = parseInt(_loc2_[_loc1_]);
  191.       if(isNaN(_loc2_[_loc1_]) || _loc2_[_loc1_] < 1)
  192.       {
  193.          trace("Debugger ::\t(setAlarms)\tThe alarm value has to be a number greater than 1.");
  194.          return 0;
  195.       }
  196.       _loc3_.alarms.push(_loc2_[_loc1_]);
  197.       _loc1_ = _loc1_ + 1;
  198.    }
  199.    _loc3_.hasMultipleAlarms = true;
  200. };
  201. Timer.prototype.getAlarms = function()
  202. {
  203.    return this.alarms;
  204. };
  205. Timer.prototype.getAccuracy = function()
  206. {
  207.    var _loc1_ = this;
  208.    return (getTimer() - _loc1_.startTime) / _loc1_.getHiddenTicksCount() - _loc1_.getDelay();
  209. };
  210. Timer.prototype.controlTimelineOf = function()
  211. {
  212.    var _loc2_ = arguments;
  213.    var _loc3_ = this;
  214.    var _loc1_ = 0;
  215.    while(_loc1_ < _loc2_.length)
  216.    {
  217.       if(_loc2_[_loc1_] instanceof movieclip != 1)
  218.       {
  219.          trace("Debugger::\t(controlTimelineOf)\tThe parameter has to be the instance name of a movie-clip.");
  220.          return 0;
  221.       }
  222.       _loc3_.mcs.push(_loc2_[_loc1_]);
  223.       _loc2_[_loc1_].stop();
  224.       _loc1_ = _loc1_ + 1;
  225.    }
  226.    _loc3_.controlling = true;
  227. };
  228. Timer.prototype.releaseTimelineOf = function(mc, stopMC)
  229. {
  230.    var _loc2_ = this;
  231.    var _loc3_ = mc;
  232.    var _loc1_ = 0;
  233.    while(_loc1_ < _loc2_.mcs.length)
  234.    {
  235.       if(_loc2_.mcs[_loc1_] == _loc3_)
  236.       {
  237.          delete _loc2_.mcs[_loc1_];
  238.       }
  239.       _loc1_ = _loc1_ + 1;
  240.    }
  241.    !stopMC ? _loc3_.play() : _loc3_.stop();
  242.    if(_loc2_.mcs.length < 1)
  243.    {
  244.       _loc2_.controlling = false;
  245.    }
  246. };
  247. Timer.prototype.schedule = function(handler, methodName, param)
  248. {
  249.    var _loc1_ = this;
  250.    _loc1_.handler = handler;
  251.    _loc1_.methodName = methodName;
  252.    _loc1_.param = param;
  253.    _loc1_.scheduled = true;
  254. };
  255. Timer.prototype.clearSchedule = function()
  256. {
  257.    this.scheduled = false;
  258. };
  259. Timer.prototype.execute = function()
  260. {
  261.    var _loc1_ = this;
  262.    _loc1_.handler[_loc1_.methodName](_loc1_.param);
  263. };
  264. Timer.prototype.tick = function()
  265. {
  266.    var _loc1_ = this;
  267.    if(_loc1_.hasAlarm)
  268.    {
  269.       if(_loc1_.getTicksCount() == _loc1_.alarm)
  270.       {
  271.          _loc1_.sendEvent("onTimerAlarm");
  272.       }
  273.    }
  274.    if(_loc1_.hasMultipleAlarms)
  275.    {
  276.       var _loc3_ = 0;
  277.       while(_loc3_ < _loc1_.alarms.length)
  278.       {
  279.          if(_loc1_.getTicksCount() == _loc1_.alarms[_loc1_.sentAlarms])
  280.          {
  281.             _loc1_.sendEvent("onTimerAlarms",_loc1_.sentAlarms++);
  282.          }
  283.          _loc3_ = _loc3_ + 1;
  284.       }
  285.    }
  286.    if(_loc1_.isPaused() && _loc1_.getTicksDifference() == _loc1_.sleepDuration)
  287.    {
  288.       _loc1_.resume();
  289.    }
  290.    if(_loc1_.hasControl())
  291.    {
  292.       var _loc2_ = 0;
  293.       while(_loc2_ < _loc1_.mcs.length)
  294.       {
  295.          _loc1_.mc = _loc1_.mcs[_loc2_];
  296.          !_loc1_.isPaused() ? _loc1_.mc.gotoAndStop(_loc1_.mc._currentframe != _loc1_.mc._totalframes ? _loc1_.mc._currentframe + 1 : _loc1_.mc._currentframe - _loc1_.mc._totalframes + 1) : _loc1_.mc.stop();
  297.          _loc2_ = _loc2_ + 1;
  298.       }
  299.       updateAfterEvent();
  300.    }
  301.    if(_loc1_.getHiddenTicksCount() >= _loc1_.initialDelay)
  302.    {
  303.       if(!_loc1_.isPaused() && _loc1_.isRunning())
  304.       {
  305.          _loc1_.sendEvent("onTimerTick",++_loc1_.ticksCounter);
  306.       }
  307.    }
  308.    if(_loc1_.isRunning())
  309.    {
  310.       _loc1_.hiddenTicksCounter = _loc1_.hiddenTicksCounter + 1;
  311.    }
  312.    if(_loc1_.hasSchedule() && _loc1_.getHiddenTicksCount() >= _loc1_.initialDelay)
  313.    {
  314.       _loc1_.execute();
  315.    }
  316. };
  317.